home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_vms.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  259 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_vms.c,v 1.6 92/10/21 13:42:22 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library VMS-specific Routines.
  31.  */
  32. #include "tiffiop.h"
  33.  
  34. static int
  35. _tiffReadProc(void* fd, char* buf, u_long size)
  36. {
  37.     return (read((int) fd, buf, size));
  38. }
  39.  
  40. static int
  41. _tiffWriteProc(void* fd, char* buf, u_long size)
  42. {
  43.     return (write((int) fd, buf, size));
  44. }
  45.  
  46. static int
  47. _tiffSeekProc(void* fd, long off, int whence)
  48. {
  49.     return (lseek((int) fd, off, whence));
  50. }
  51.  
  52. static int
  53. _tiffCloseProc(void* fd)
  54. {
  55.     return (close((int) fd));
  56. }
  57.  
  58. #include <sys/stat.h>
  59.  
  60. static long
  61. _tiffSizeProc(void* fd)
  62. {
  63.     struct stat sb;
  64.     return (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
  65. }
  66.  
  67. #ifdef MMAP_SUPPORT
  68. #include <fab.h>
  69. #include <secdef.h>
  70.  
  71. /*
  72.  * Table for storing information on current open sections. 
  73.  * (Should really be a linked list)
  74.  */
  75. #define MAX_MAPPED 100
  76. static int no_mapped = 0;
  77. static struct {
  78.     char *base;
  79.     char *top;
  80.     unsigned short channel;
  81. } map_table[MAX_MAPPED];
  82.  
  83. /* 
  84.  * This routine maps a file into a private section. Note that this 
  85.  * method of accessing a file is by far the fastest under VMS.
  86.  * The routine may fail (i.e. return 0) for several reasons, for
  87.  * example:
  88.  * - There is no more room for storing the info on sections.
  89.  * - The process is out of open file quota, channels, ...
  90.  * - fd does not describe an opened file.
  91.  * - The file is already opened for write access by this process
  92.  *   or another process
  93.  * - There is no free "hole" in virtual memory that fits the
  94.  *   size of the file
  95.  */
  96. static int
  97. _tiffMapProc(void* fd, char** pbase, long* psize)
  98. {
  99.     char name[256];
  100.     struct FAB fab;
  101.     unsigned short channel;
  102.     char *inadr[2], *retadr[2];
  103.     unsigned long status;
  104.     long size;
  105.     
  106.     if (no_mapped >= MAX_MAPPED)
  107.         return(0);
  108.     /*
  109.      * We cannot use a file descriptor, we
  110.      * must open the file once more.
  111.      */
  112.     if (getname(fd, name, 1) == NULL)
  113.         return(0);
  114.     /* prepare the FAB for a user file open */
  115.     fab = cc$rms_fab;
  116.     fab.fab$v_ufo = 1;
  117.     fab.fab$b_fac = FAB$M_GET;
  118.     fab.fab$b_shr = FAB$M_SHRGET;
  119.     fab.fab$l_fna = name;
  120.     fab.fab$b_fns = strlen(name);
  121.     status = sys$open(&fab);    /* open file & get channel number */
  122.     if ((status&1) == 0)
  123.         return(0);
  124.     channel = (unsigned short)fab.fab$l_stv;
  125.     inadr[0] = inadr[1] = (char *)0; /* just an address in P0 space */
  126.     /*
  127.      * Map the blocks of the file up to
  128.      * the EOF block into virtual memory.
  129.      */
  130.     size = _tiffSizeProc(fd);
  131.     status = sys$crmpsc(inadr, retadr, 0, SEC$M_EXPREG, 0,0,0, channel,
  132.         howmany(size,512), 0,0,0);
  133.     if ((status&1) == 0){
  134.         sys$dassgn(channel);
  135.         return(0);
  136.     }
  137.     *pbase = retadr[0];        /* starting virtual address */
  138.     /*
  139.      * Use the size of the file up to the
  140.      * EOF mark for UNIX compatibility.
  141.      */
  142.     *psize = size;
  143.     /* Record the section in the table */
  144.     map_table[no_mapped].base = retadr[0];
  145.     map_table[no_mapped].top = retadr[1];
  146.     map_table[no_mapped].channel = channel;
  147.     no_mapped++;
  148.  
  149.         return(1);
  150. }
  151.  
  152. /*
  153.  * This routine unmaps a section from the virtual address space of 
  154.  * the process, but only if the base was the one returned from a
  155.  * call to TIFFMapFileContents.
  156.  */
  157. static void
  158. _tiffUnmapProc(void* fd, char* base, long size)
  159. {
  160.     char *inadr[2];
  161.     int i, j;
  162.     
  163.     /* Find the section in the table */
  164.     for (i = 0;i < no_mapped; i++) {
  165.         if (map_table[i].base == base) {
  166.             /* Unmap the section */
  167.             inadr[0] = base;
  168.             inadr[1] = map_table[i].top;
  169.             sys$deltva(inadr, 0, 0);
  170.             sys$dassgn(map_table[i].channel);
  171.             /* Remove this section from the list */
  172.             for (j = i+1; j < no_mapped; j++)
  173.                 map_table[j-1] = map_table[j];
  174.             no_mapped--;
  175.             return;
  176.         }
  177.     }
  178. }
  179. #else /* !MMAP_SUPPORT */
  180. static int
  181. _tiffMapProc(void* fd, char** pbase, long* psize)
  182. {
  183.     return (0);
  184. }
  185.  
  186. static void
  187. _tiffUnmapProc(void* fd, char* base, long size)
  188. {
  189. }
  190. #endif /* !MMAP_SUPPORT */
  191.  
  192. /*
  193.  * Open a TIFF file descriptor for read/writing.
  194.  */
  195. TIFF *
  196. TIFFFdOpen(int fd, const char* name, const char* mode)
  197. {
  198.     TIFF *tif;
  199.  
  200.     tif = TIFFClientOpen(name, mode,
  201.         (void*) fd,
  202.         _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
  203.         _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
  204.     if (tif)
  205.         tif->tif_fd = fd;
  206.     return (tif);
  207. }
  208.  
  209. /*
  210.  * Open a TIFF file for read/writing.
  211.  */
  212. TIFF *
  213. TIFFOpen(const char* name, const char* mode)
  214. {
  215.     static const char module[] = "TIFFOpen";
  216.     int m, fd;
  217.  
  218.     m = _TIFFgetMode(mode, module);
  219.     if (m == -1)
  220.         return ((TIFF *)0);
  221.         if (m&O_TRUNC){
  222.                 /*
  223.          * There is a bug in open in VAXC. If you use
  224.          * open w/ m=O_RDWR|O_CREAT|O_TRUNC the
  225.          * wrong thing happens.  On the other hand
  226.          * creat does the right thing.
  227.                  */
  228.                 fd = creat(name, 0666,
  229.             "alq = 128", "deq = 64", "mbc = 32",  "fop = tef");
  230.     } else if (m&O_RDWR) {
  231.         fd = open(name, m, 0666,
  232.             "deq = 64", "mbc = 32", "fop = tef");
  233.     } else
  234.         fd = open(name, m, 0666, "mbc = 32");
  235.     if (fd < 0) {
  236.         TIFFError(module, "%s: Cannot open", name);
  237.         return ((TIFF *)0);
  238.     }
  239.     return (TIFFFdOpen(fd, name, mode));
  240. }
  241.  
  242. void *
  243. _TIFFmalloc(size_t s)
  244. {
  245.     return (malloc(s));
  246. }
  247.  
  248. void
  249. _TIFFfree(void* p)
  250. {
  251.     free(p);
  252. }
  253.  
  254. void *
  255. _TIFFrealloc(void* p, size_t s)
  256. {
  257.     return (realloc(p, s));
  258. }
  259.